import axios from 'axios'
import {
  Indicator,
  Toast
} from 'mint-ui';
import router from './router'
import {
  $api,
  $pay
} from './config.js'

// 请求拦截  设置统一header
axios.interceptors.request.use(config => {
  //加载动画
  Indicator.open({
    text: '加载中...',
    spinnerType: 'fading-circle'
  }); //''
  if (config.url == $pay + '/mobilePage/webpay.aspx' ||
    config.url == $pay + '/mobilePage/EncryptWx.aspx' ||
    config.url == $pay + '/mobilePage/WxPay.aspx' ||
    config.url == $api + '/oauth2/token') {
    config.headers = {
      'Content-Type': 'application/x-www-form-urlencoded',
    }


  } else {

    if (localStorage.zyLoginToken && localStorage.zyLoginToken != undefined) {
      config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': localStorage.zyLoginToken
      }
    } else if (localStorage.zyToken && localStorage.zyToken != undefined) {
      config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
        'Authorization': localStorage.zyToken
      }
    } else {
      config.headers = {
        'Content-Type': 'application/x-www-form-urlencoded',
      }
    }
  }
  return config
}, error => {
  return new Promise.reject(error)
})

//响应拦截
axios.interceptors.response.use(config => {
  Indicator.close();
  return config
}, error => {
  Toast({
    message: error.response.data.Message,
    duration: 2000
  });
  Indicator.close();
  if (error.response.status == '401') {
    // Toast({
    //   message: error.response.statusText,
    // })
    // router.push("/login");
  }
  // else {
  //   Toast({
  //     message: error.response.statusText,
  //   })
  // }
  return Promise.reject(error)
})


export default axios
